package annotations.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RunTestsBad {

	public static void main(String[] args) throws ClassNotFoundException {
		int tests = 0;
		int passed = 0;
		
		Class<?> testClass = SampleBad.class;
		for (Method method : testClass.getDeclaredMethods()) {
			if (method.isAnnotationPresent(ExceptionTestBad.class)) {
				tests++;
				
				try {
					method.invoke(null);
					System.out.printf("Test %s failed : no exception %n", method);
				} catch (InvocationTargetException e) {
					Throwable exc = e.getCause();
					int oldPass = passed;
					Class<? extends Throwable>[] excTypes = method.getAnnotation(ExceptionTestBad.class).value();
					for (Class<? extends Throwable> excType : excTypes) {
						if (excType.isInstance(exc)) {
							passed++;
							break;
						}
					}
					if (oldPass == passed) {
						System.out.printf("Test %s failed: %s%n", method, exc);
					}
				} catch (Exception e) {
					System.out.println("Invalid @Test: " + method);
				}
			}
		}
		
		System.out.printf("Passed: %d, Failed: %d%n", passed, tests - passed);
	}
}
